﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.OpenAccess;
using vmModel;
using XenAPI;
using System.Threading;
using Host = XenAPI.Host;

namespace ITMan
{
    public partial class FormVmSet : Form
    {
        public FormVmSet()
        {
            InitializeComponent();
        }

        private EditMode mode;

        private enum EditMode
        {
            Normal = 0,
            Edit = 1,
            Add = 2
        }

        private void ChangeEditMode(EditMode editMode)
        {
            switch (editMode)
            {
                case EditMode.Normal:
                    {
                        dataGridView1.Enabled = true;
                        mode = EditMode.Normal;

                        btAdd.Enabled = true;
                        btEdit.Enabled = true;
                        btSave.Enabled = false;
                        btCancel.Enabled = false;

                        tbHostID.Enabled = false;
                        tbIPv4.Enabled = false;
                        tbIPv6.Enabled = false;
                        tbUsername.Enabled = false;
                        tbPassword.Enabled = false;
                        tbVersion.Enabled = false;
                    }
                    break;
                case EditMode.Edit:
                    {
                        dataGridView1.Enabled = true;
                        mode = EditMode.Edit;

                        btAdd.Enabled = false;
                        btEdit.Enabled = false;
                        btSave.Enabled = true;
                        btCancel.Enabled = true;

                        tbHostID.Enabled = false;
                        tbIPv4.Enabled = true;
                        tbIPv6.Enabled = false;
                        tbUsername.Enabled = true;
                        tbPassword.Enabled = true;
                        tbVersion.Enabled = false;
                    }
                    break;
                case EditMode.Add:
                    {
                        dataGridView1.Enabled = false;
                        mode = EditMode.Add;

                        btAdd.Enabled = false;
                        btEdit.Enabled = false;
                        btSave.Enabled = true;
                        btCancel.Enabled = true;

                        tbHostID.Enabled = false;
                        tbIPv4.Enabled = true;
                        tbIPv6.Enabled = false;
                        tbUsername.Enabled = true;
                        tbPassword.Enabled = true;
                        tbVersion.Enabled = false;

                    }
                    break;
            }
        }

        BindingSource vmBindingSource=new BindingSource();

        private void InitGrid()
        {
            var dbContext = new vmEntities();
            IQueryable<vmModel.Host> hosts = from i in dbContext.Hosts
                select i;
            if (hosts.Any())
            {
                vmBindingSource.DataSource = hosts;
            }
        }

        private void InitEditZone()
        {
            tbHostID.DataBindings.Clear();
            tbIPv4.DataBindings.Clear();
            tbIPv6.DataBindings.Clear();
            tbUsername.DataBindings.Clear();
            tbPassword.DataBindings.Clear();
            tbVersion.DataBindings.Clear();

            tbHostID.DataBindings.Add("Text", vmBindingSource, "Hostid");
            tbIPv4.DataBindings.Add("Text", vmBindingSource, "Ipv4");
            tbIPv6.DataBindings.Add("Text", vmBindingSource, "Ipv6");
            tbUsername.DataBindings.Add("Text", vmBindingSource, "Username");
            tbPassword.DataBindings.Add("Text", vmBindingSource, "Password");
            tbVersion.DataBindings.Add("Text", vmBindingSource, "Version");
        }

        private void FormVmSet_Load(object sender, EventArgs e)
        {
            InitGrid();
            InitEditZone();
            ChangeEditMode(EditMode.Normal);

            dataGridView1.DataSource = vmBindingSource;
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            ChangeEditMode(EditMode.Add);

            tbHostID.DataBindings.Clear();
            tbIPv4.DataBindings.Clear();
            tbIPv6.DataBindings.Clear();
            tbUsername.DataBindings.Clear();
            tbPassword.DataBindings.Clear();
            tbVersion.DataBindings.Clear();

            tbIPv4.Text = null;
            tbUsername.Text = null;
            tbPassword.Text = null;
            tbVersion.Text = null;
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            ChangeEditMode(EditMode.Edit);
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbIPv4.Text) || string.IsNullOrEmpty(tbUsername.Text) ||
                string.IsNullOrEmpty(tbPassword.Text))
            {
                MessageBox.Show("请检查数据是否完整！");
            }
            else
            {
                switch (mode)
                {
                    case EditMode.Add:
                    {
                        var dbContext = new vmEntities();

                        vmModel.Host host = new vmModel.Host();
                        host.Ipv4 = tbIPv4.Text;
                        host.Username = tbUsername.Text;
                        host.Password = tbPassword.Text;

                        dbContext.Add(host);
                        dbContext.SaveChanges();
                        
                    }break;
                    case EditMode.Edit:
                        {
                            var dbContext = new vmEntities();

                            IQueryable<vmModel.Host> hosts = from i in dbContext.Hosts
                                where i.Hostid == int.Parse(tbHostID.Text)
                                select i;
                            if (hosts.Any())
                            {
                                vmModel.Host host = hosts.FirstOrDefault();
                                host.Ipv4 = tbIPv4.Text;
                                host.Username = tbUsername.Text;
                                host.Password = tbPassword.Text;

                                dbContext.SaveChanges();
                            }

                        } break;
                        
                }
            }
            FormVmSet_Load(sender,e);
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            ChangeEditMode(EditMode.Normal);
            FormVmSet_Load(sender, e);
        }

        private void btGetUuid_Click(object sender, EventArgs e)
        {
            var dbContext = new vmEntities();
            foreach (vmModel.Host v in vmBindingSource)
            {
                IQueryable<vmModel.Host> myHosts = from i in dbContext.Hosts
                                                  where i.Ipv4 == v.Ipv4
                                                  select i;
                vmModel.Host myHost = myHosts.FirstOrDefault();

                Session session=new Session(v.Ipv4,80);

                try
                {
                    session.login_with_password(v.Username, v.Password);
                    List<XenRef<XenAPI.Host>> HostRefs = XenAPI.Host.get_all(session);

                    foreach (XenRef<Host> hostRef in HostRefs)
                    {
                        XenAPI.Host host = XenAPI.Host.get_record(session, hostRef);
                        myHost.Version = host.software_version["product_version"].ToString();
                        myHost.Uuid = host.uuid;
                        dbContext.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw;
                }
            }
            FormVmSet_Load(sender, e);
            MessageBox.Show("获取完成");
        }
    }
}
